<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/static/xadmin/css/font.css">
    <link rel="stylesheet" href="/static/xadmin/css/xadmin.css">
    <link rel="stylesheet" href="/static/css/apublic.css">
    <script src="/static/xadmin/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/xadmin/js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="/static/xadmin/html5.min.js"></script>
    <script src="/static/xadmin/respond.min.js"></script>
    <![endif]-->
    <script src="/static/until.js" charset="utf-8"></script>
    <script src="/static/vue.js" charset="utf-8"></script>
    <script>
        if(!sessionStorage.getItem('token')) window.top.location.href='/login.html';
    </script>
    <style>
        body{
            background:#fff;
        }
        [v-cloak] {
            display: none !important;
        }
        .layui-icon-close{
            cursor:pointer;
        }
        .layui-icon-close:hover{
            color:red;
        }
        input{
            border-width: 1px;
            border-style: solid;
            background-color: #fff;
            border-radius: 2px;
            border-color: #e6e6e6;
            padding:3px 5px;
            width:70%;
        }
        .line{
            padding:2px 0px;
        }
    </style>
</head>
<body>
<div v-cloak class="layui-fluid" id="app">
    <div class="layui-row">
        <div class="attr">
                <div class="item-val">
                    名称：
                    <input :value="goods_attr.goods_attribute.title" v-model="goods_attr.goods_attribute.title">
                </div>
                <div>
                    <div class="line" v-for="(item,index) in goods_attr.goods_attribute.goods_attribute_value">
                        &nbsp;&nbsp;值{{index+1}}：
                        <input v-model="item.title">
                        <span v-on:click="delval(index)">删除</span>
                    </div>
                </div>
                
            
        </div>
        <div style="text-align: center;margin-top:5px;">
            <span class="layui-btn layui-btn-small" v-on:click="add">保存</span>
            <span class="layui-btn layui-btn-small" v-on:click="addval">添加值</span>
        </div>
    </div>
</div>
<script>
    layui.use(['layer','jquery'],
        function() {
            $ = layui.jquery;
            var app = new Vue({
                el: '#app',
                data: {
                    goods_attr:{
                        goods_id:Number(getQueryString("id")),
                        attribute_id:0,
                        goods_attribute:{
                            id:0,
                            title:"",
                            goods_attribute_value:[]
                        }
                    },
                    loading:null
                },
                created:function(){
                },
                methods:{
                    add(){
                        var that=this
                        layer.load(); 
                        if(that.goods_attr.goods_attribute.title.length==0){
                            layer.alert("名称不能为空", {icon: 5})
                            return
                        }
                        if(that.goods_attr.goods_attribute.goods_attribute_value.length==0){
                            layer.alert("请添加值", {icon: 5})
                            return
                        }
                        for(var i=0;i<that.goods_attr.goods_attribute.goods_attribute_value.length;i++){
                            if(that.goods_attr.goods_attribute.goods_attribute_value[i].title.length==0){
                                layer.alert("值名称不能为空", {icon: 5})
                                return
                            }
                        }
                        u_post("/goods/add_attr",JSON.stringify(that.goods_attr),function(res){
                            layer.closeAll('loading');
                            if(res.status.errorCode==0){
                                that.goods_attr.goods_attribute.title=""
                                that.goods_attr.goods_attribute.goods_attribute_value=[]
                                window.parent.getgoods()
                                layer.msg("完成")
                            }else{
                                layer.alert(res.status.msg, {icon: 5})
                            }
                        })
                    },
                    addval(){
                        this.goods_attr.goods_attribute.goods_attribute_value.push(
                                                                {
                                                                    "id": 0,
                                                                    "attribute_id": 0,
                                                                    "title": ""
                                                                })
                    },
                    delval(index){
                        this.goods_attr.goods_attribute.goods_attribute_value.splice(index,1)
                    }
                }
            }) 

        })
    
</script>
</body>

</html>
